package com.topc.platform.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.topc.base.exception.Asserts;
import com.topc.base.model.PageParams;
import com.topc.base.model.PageResult;
import com.topc.base.utils.PageUtils;
import com.topc.platform.mapper.CarouselInfoMapper;
import com.topc.platform.model.po.CarouselInfo;
import com.topc.platform.model.vo.IndexCarouselShowVo;
import com.topc.platform.service.CarouselInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 轮播图 服务实现类
 * </p>
 *
 * @author Gogoing
 */
@Slf4j
@Service
public class CarouselInfoServiceImpl extends ServiceImpl<CarouselInfoMapper, CarouselInfo> implements CarouselInfoService {

    @Override
    public void saveInfo(String imgUrl, String linkUrl) {
        CarouselInfo carouselInfo = new CarouselInfo();
        carouselInfo.setImgUrl(imgUrl);
        carouselInfo.setLinkUrl(linkUrl);
        boolean save = this.save(carouselInfo);
        if(!save) Asserts.fail("创建失败");
    }

    @Override
    public void removeInfoById(Long id) {
        boolean remove = this.removeById(id);
        if (!remove) Asserts.fail("删除失败");
    }

    @Override
    public Boolean updateImgById(Long id, String imgUrl) {
        CarouselInfo carouselInfo = new CarouselInfo();
        carouselInfo.setId(id);
        carouselInfo.setImgUrl(imgUrl);
        boolean remove = this.updateById(carouselInfo);
        if (!remove) return false;
        return true;
    }

    @Override
    public void updateInfoById(Long id, String linkUrl,int status) {
        CarouselInfo carouselInfo = new CarouselInfo();
        carouselInfo.setId(id);
        carouselInfo.setLinkUrl(linkUrl);
        carouselInfo.setStatus(status);
        boolean remove = this.updateById(carouselInfo);
        if (!remove) Asserts.fail("更新失败");
    }

    @Override
    public List<IndexCarouselShowVo> getIndexCarouselShowVoList() {
        LambdaQueryWrapper<CarouselInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CarouselInfo::getStatus,1);
        //取出所有轮播图信息
        List<CarouselInfo> list = this.list(queryWrapper);
        //将轮播图部分信息导入轮播图展示所需信息的集合
        List<IndexCarouselShowVo> collect = list.stream().map(carouselInfo -> {
            IndexCarouselShowVo indexCarouselShowVo = new IndexCarouselShowVo();
            indexCarouselShowVo.setImgUrl(carouselInfo.getImgUrl());
            indexCarouselShowVo.setLinkUrl(carouselInfo.getLinkUrl());
            return indexCarouselShowVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public PageResult<CarouselInfo> searchByPage(PageParams pageParams,int status) {
        LambdaQueryWrapper<CarouselInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(CarouselInfo::getId, CarouselInfo::getImgUrl, CarouselInfo::getLinkUrl,CarouselInfo::getUpdateTime,CarouselInfo::getStatus)
                .eq(ObjectUtil.notEqual(status,-1),CarouselInfo::getStatus,status)
                .orderByDesc(CarouselInfo::getUpdateTime);
        Page page = this.page(pageParams.page(), lambdaQueryWrapper);
        return PageUtils.pageToResult(page);
    }

    @Override
    public void updateInfo(Long id,String imgUrl, String linkUrl) {
        LambdaUpdateWrapper<CarouselInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(CarouselInfo::getId, id);
        CarouselInfo carouselInfo = new CarouselInfo();
        carouselInfo.setLinkUrl(linkUrl);
        carouselInfo.setImgUrl(imgUrl);
        boolean update = this.update(carouselInfo, updateWrapper);
        if (!update) Asserts.fail("更新失败");
    }
}
